import axios from 'axios'

const request = axios.create({
    timeout: 3000
})

// 请求拦截器
request.interceptors.request.use(config => {
    // 获取token
    const token = localStorage.getItem('token')
    // 添加token
    if(token) {
        config.headers = {
            ...config.headers,
            Authorization: token
        }
    }

    return config
})

// 响应拦截
request.interceptors.response.use((config) => {
    return config.data
},(err) => {


    // 处理错误状态
    if (err.response.status === 404) {
        window.location.href = '/404'
    } else if (err.response.status === 401) {
        window.location.href = '/login'
    }


    return Promise.reject(err)
})

export default request

